package com.lagou.service.impl;

import com.lagou.dao.CodeDao;
import com.lagou.feign.EmailFeignClient;
import com.lagou.feign.UserFeignClient;
import com.lagou.pojo.LagouAuthCode;
import com.lagou.service.CodeService;
import com.lagou.util.StringUtils;
import org.hibernate.criterion.Example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class CodeServiceImpl implements CodeService {
    private static final Map<String, Long> CODE_MAP = new HashMap<>();

    @Autowired
    private CodeDao codeDao;
    @Autowired
    private EmailFeignClient emailFeignClient;

    @Autowired
    private UserFeignClient userFeignClient;

    /**
     * 生成验证码并发送到对应邮箱，0.发送成功 1.发送失败 2.已注册 3.请一分钟后再试
     *
     * @param email
     * @return
     */
    @Override
    public String create(String email, String clientIp) {
        //判断是否已经注册过
        boolean registered = userFeignClient.isRegistered(email);
        if (registered) {
            return "2";
        }
        //判断1分钟内是否发送过
        Long sendTime = CODE_MAP.get(clientIp);
        //如果该客户端发送过验证码，并且距离现在在一分钟之内，则返回
        if (sendTime != null && System.currentTimeMillis() - sendTime < 60000L) {
            return "3";
        }


        //生成验证码
        String validCode = StringUtils.getValidCode(6);
        //调用email微服务发送验证码
        boolean result = emailFeignClient.sendCode(email, validCode);
        if (!result) {
            return "1";
        }
        //发送成功后，保存时间戳
        CODE_MAP.put(clientIp,System.currentTimeMillis());
        LagouAuthCode lagouAuthCode = new LagouAuthCode();
        lagouAuthCode.setCode(validCode);
        lagouAuthCode.setEmail(email);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date date = new Date();
        String createTime = simpleDateFormat.format(date);
        Date afterTenMinuteDate = new Date(date.getTime() + 600000); //十分钟后的时间
        String expireTime = simpleDateFormat.format(afterTenMinuteDate);
        lagouAuthCode.setCreateTime(createTime);
        lagouAuthCode.setExpireTime(expireTime);
        //保存数据库
        codeDao.save(lagouAuthCode);
        return "0";
    }

    @Override
    public String validate(String email, String code) {
        LagouAuthCode lastAuthCode = codeDao.getLastAuthCode(email);
        //未发送过验证码
        if (lastAuthCode == null) {
            return "3";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date expireTime = simpleDateFormat.parse(lastAuthCode.getExpireTime());
            long time = expireTime.getTime();
            //超时
            if (time < System.currentTimeMillis()) {
                return "2";
            }
            //验证码错误
            if (!lastAuthCode.getCode().equals(code)) {
                return "1";
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "0";
    }
}
